package com.wanchuang.shengfubao.module.main;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.adapter.MessageAdapter;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.MemberOuterClass;
import com.wanchuang.shengfubao.utils.StatusBarUtil;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageActivity extends BaseActivity {

    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private int pagesize = 10;  // 当前viewPage的位置
    private int page = 1; // 当前页数
    private MessageAdapter mAdapter;
    private List<MemberOuterClass.System_log.System_log_list> datas = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        initRecyclerView();
        initListener();
        tvTitle.setText("通知");
        loadData();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());

    }

    private void loadData() {
        NetworkManager.INSTANCE.post(Apis.system_log,page, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                MemberOuterClass.System_log data = MemberOuterClass.System_log.parseFrom(response);
                List<MemberOuterClass.System_log.System_log_list> lists = data.getSystemLogListList();
                mAdapter.addData(lists);
                int size = lists.size();
                if (size < 10) {
                    mAdapter.getLoadMoreModule().loadMoreEnd(true);
                } else {
                    mAdapter.getLoadMoreModule().loadMoreComplete();
                }
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
//                showToast(errorMessage);
            }
        });


    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new MessageAdapter(mContext, datas));
        mAdapter.setEmptyView(R.layout.recycle_empty);
        // 每个item的点击事件
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
//            WebViewActivity.startActivity(mContext, WebApis. + "?member_id=" + datas.get(position).getMemberId());
        });
        mAdapter.getLoadMoreModule().setOnLoadMoreListener(() -> {
            page = mAdapter.getData().size() / pagesize + 1;
            loadData();
        });

    }


}
